package log

import (
	"pan/common/util"
	"strings"

	"github.com/gookit/color"
)

type LogLevel int

const (
	LogDebug LogLevel = iota + 1
	LogInfo
	LogWarning
	LogError
)

const (
	LogDebugName   = "debug"
	LogInfoName    = "info"
	LogWarningName = "warning"
	LogErrorName   = "error"
)

var logLevel = LogDebug

func Level() LogLevel {
	return logLevel
}

func LevelName() string {
	switch logLevel {
	case LogDebug:
		return LogDebugName
	case LogInfo:
		return LogInfoName
	case LogWarning:
		return LogWarningName
	case LogError:
		return LogErrorName
	default:
		return "Unknown"
	}
}

func SetLevel(lv LogLevel) {
	logLevel = lv
}

func SetLevelName(name string) {
	if name == "" {
		return
	}
	lv := strings.ToLower(strings.TrimSpace(name))
	switch lv {
	case LogDebugName:
		logLevel = LogDebug
	case LogInfoName:
		logLevel = LogInfo
	case LogWarningName:
		logLevel = LogWarning
	case LogErrorName:
		logLevel = LogError
	}
}

func Debug(msg ...interface{}) {
	if IsDebugEnable() {
		color.Gray.Println(util.SlicePrepend(msg, "DEBUG |")...)
	}
}

func Info(msg ...interface{}) {
	if IsInfoEnable() {
		color.Info.Println(msg...)
	}
}

func Warn(msg ...interface{}) {
	if IsWarningEnable() {
		color.Warn.Println(msg...)
	}
}

func Error(msg ...interface{}) {
	if IsErrorEnable() {
		color.FgRed.Println(msg...)
	}
}

func IsDebugEnable() bool {
	return logLevel <= LogDebug
}

func IsInfoEnable() bool {
	return logLevel <= LogInfo
}

func IsWarningEnable() bool {
	return logLevel <= LogWarning
}

func IsErrorEnable() bool {
	return logLevel <= LogError
}
